#!/usr/bin/env python
# encoding:utf-8
# @Time   : 2019/11/29
# @Author : 胡茂海
# @Site   : 
# @File   : FeatureProcess.py
import os
import pathlib
import sys
root_path = str(pathlib.Path(os.path.abspath(__file__)).parent.parent)
sys.path.append(root_path)

from sklearn import preprocessing as skp
from sklearn import decomposition  as dpn
from sklearn import feature_selection as fsn
import feature.feature_expand as ft
from utils.util import back_args_str, auto_pate

class FeaturesStandard(object):
    def __init__(self, method="StandardScaler()"):
        self.extra = False
        try:
            self.method = eval(f"skp.{auto_pate(method)}")
        except Exception as e:
            self.extra = True
        if self.extra:
            try:
                self.method = eval(f"ft.{auto_pate(method)}")
            except Exception as e:
                raise AttributeError('传入的方法名或者参数不对，无法实例化对象')

    def fit(self, *args,**kwargs):
        return self.method.fit(*args,**kwargs)

    def transform(self, *args,**kwargs):
        return self.method.transform(*args,**kwargs)


class FeaturesEncoder(object):
    def __init__(self, method="OneHotEncoder(handle_unknown='ignore')"):
        self.extra = False
        try:
            self.method = eval(f"skp.{auto_pate(method)}")
        except Exception as e:
            self.extra = True
        if self.extra:
            try:
                self.method = eval(f"ft.{auto_pate(method)}")
            except Exception as e:
                raise AttributeError('传入的方法名或者参数不对，无法实例化对象')

    def fit(self, *args,**kwargs):
        return self.method.fit(*args,**kwargs)

    def transform(self, *args,**kwargs):
        return self.method.transform(*args,**kwargs)

    def get_feature_names(self, *args,**kwargs):
        return self.method.get_feature_names(*args,**kwargs)


class FeaturesDecomposition(object):
    def __init__(self, method="PCA(n_components=2)"):
        self.extra = False
        try:
            self.method = eval(f"dpn.{auto_pate(method)}")
        except Exception as e:
            self.extra = True
        if self.extra:
            try:
                self.method = eval(f"ft.{auto_pate(method)}")
            except Exception as e:
                raise AttributeError('传入的方法名或者参数不对，无法实例化对象')

    def fit(self, *args,**kwargs):
        return self.method.fit(*args,**kwargs)

    def transform(self, *args,**kwargs):
        return self.method.transform(*args,**kwargs)


class FeaturesSelection(object):
    def __init__(self, method="VarianceThreshold(threshold=0.16)"):
        self.extra = False
        try:
            self.method = eval(f"fsn.{auto_pate(method)}")
        except Exception as e:
            self.extra = True
        if self.extra:
            try:
                self.method = eval(f"ft.{auto_pate(method)}")
            except Exception as e:
                raise AttributeError('传入的方法名或者参数不对，无法实例化对象')

    def fit(self, *args,**kwargs):
        return self.method.fit(*args,**kwargs)

    def transform(self, *args,**kwargs):
        return self.method.transform(*args,**kwargs)


if __name__ == '__main__':
    pass